import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.sql.Array;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.sql.SQLException;

public class GenerateStarRecordsAndAssociateWithMovies {
	
	private static Connection connection = null;
	private static FileOutputStream outFile = null;
    private static PrintStream fileData = null;
	
    //arguments given by user
    private static int numberOfRecords = 0;
	private static String nameOfFile = "";
	
	//current maxId of movies table.
	private static int maxStarId = 0;
	
	// args: filename to output, number of records created
	public static void main(String args[])
	{
	    try {
		    Class.forName("org.postgresql.Driver").newInstance();
		    connection = DriverManager.getConnection(
					"jdbc:postgresql://localhost/fabflixs", "testuser", "testpass");
		    } 
		    catch(ClassNotFoundException e3)
		    {
		    	System.out.println("ClassNotFoundException: " + e3.getMessage());
		    
		    }
		    catch 
		    (IllegalAccessException e) 
		    	{
		    		System.out.println("Sorry, connection to FabFlix was denied.");
		    		
		    	}
		    catch (InstantiationException e1)
		    {
		    	System.out.println("Sorry, connection to FabFlixs has failed.");
		    	
		    }
		    catch (SQLException e2)
		    {
		    	System.out.println("Connection failed because: " + e2.getMessage());
		    	
		    }
		    catch (Exception e)
		    {
		    	System.out.println("Connection failed -> Exception: " + e.getMessage());
		    }
		    nameOfFile = args[0];
		    numberOfRecords = Integer.parseInt(args[1]);
		    
		    createStarRecords(nameOfFile);
	}


	private static void createStarRecords(String name) {
        try {
            outFile = new FileOutputStream(name);
            fileData = new PrintStream( outFile );
            
    	    String query = "Select max(id) AS max_id from stars";
    		Statement select0 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);		
    		ResultSet resultMaxId = select0.executeQuery(query);
    		
    		if(resultMaxId.next())
    			maxStarId = resultMaxId.getInt("max_id");
    		
    		String first_name= "Mood";
    		String last_name = "Le";
    		String dob = "2009/09/09";
    		String photoURL = "www.fakephotourl.png";
    		int id = maxStarId + 1;
    		for(int i = 0; i < numberOfRecords; i ++)
    		{
    			fileData.println(id + ", " +first_name+id+ ", " + last_name+id+ ", " +dob + ", " +photoURL);
    			id++;
    		}
    		
        }
        
        catch (IOException e) {
            e.printStackTrace();
            System.err.println("init: Errors accessing output file: "+e.getMessage());
            System.exit(-2);
        } catch (Exception e) {
        	  System.err.println("SQLException: "+e.getMessage());
			e.printStackTrace();
			System.exit(-2);
		}
        
        System.out.println("Done creating stars records file");
        
        createStarsInMoviesRecords();
        
	}


	private static void createStarsInMoviesRecords() {
        try {
            FileOutputStream outFile1 = new FileOutputStream("starsinmovies.txt");
            PrintStream fileData1 = new PrintStream( outFile1 );
            
    	    String query = "Select min(id) AS min_id, max(id) AS max_id from movies";
    		Statement select0 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);		
    		ResultSet resultMaxId = select0.executeQuery(query);
    		
    		int minMovieId = 0;
    		int maxMovieId = 0;
    		if(resultMaxId.next())
    		{
    			minMovieId = resultMaxId.getInt("min_id");
    			maxMovieId = resultMaxId.getInt("max_id");
    			
    		}
    		
    		int id = maxStarId + 1;
    		int middleMovieId = maxMovieId/2;
    		for(int i = 0; i < numberOfRecords; i ++)
    		{
    			fileData1.println(id + ", " +minMovieId);
    			fileData1.println(id + ", " +middleMovieId);
    			fileData1.println(id + ", " +maxMovieId);
    			id++;
    		}
    		
        }
        
        catch (IOException e) {
            e.printStackTrace();
            System.err.println("init: Errors accessing output file: "+e.getMessage());
            System.exit(-2);
        } catch (Exception e) {
        	  System.err.println("SQLException: "+e.getMessage());
			e.printStackTrace();
			System.exit(-2);
		}
        
        System.out.println("Done outputting starsinmovies records file.");
		
	}
}
